import Axios from '@/util/ajax'

const state = {
    isInitialized: false,
    //保存报价所有页面都会用到的一些值 ：如下拉框的列表值等,
    initPageData: {
        customers: [], // 客户列表
        sells:[], // 销售人员
        receiptCompanies:[],  // 开票公司
        statusList:[],
        units:[], //数量单位
        canCloseOrder: false
    },
    showEditOrder: false,
    showOrderItemDialog: false,
    orderItems: [],
    orderId: -1,  // 新增或修改的报价
    orderItem: {},
    selectedItems: [],
    isView: false,
    isViewItem: false

};

// 通过 mutation 去更改 state 里面的值
const mutations = {
    Set_Show_Edit_Order: (state, data) => {
        state.showEditOrder = data
    },

    Set_Order_Items: (state, data) => {
        state.orderItems = data
    },

    Show_Add_Order_Item: (state) => {
        state.orderItem = {
            id: -1,
            name: "",
            material: "",
            unit: "G",
            orderId: state.orderId,
            remark: ""
        };
        state.showOrderItemDialog = true;
        state.isView = false;
        state.isViewItem = false;
    },

    Show_Edit_Order_Item: (state, orderItem) => {
        Object.assign(state.orderItem, orderItem);
        state.showOrderItemDialog = true;
        state.isView = false;
        state.isViewItem = false;
    },

    Show_View_Order_Item: (state, orderItem) => {
        state.orderItem = orderItem;
        state.showOrderItemDialog = true;
        state.isViewItem = true;
    },

    Hide_Order_Item_Dialog: (state) => {
        state.showOrderItemDialog = false
    },

    Set_Selected_Item: (state, data) => {
        state.selectedItems = data
    },
};

// 通过 action 去 commit mutation
const actions = {
    async loadInitPageData({state}) {
        //if(!state.isInitialized){
            state.showEditOrder = false;
            let res = await Axios.get('/sell/order/loadInitPageData', {});
            //console.log("初始化查询订单页面：", res.result);
            state.initPageData = res.result;
            //console.log("initPageData.customers: ", state.initPageData.customers);
            state.isInitialized = true;
        //}
    },

    showEditOrder({state}, orderId) {
        state.showEditOrder = true;
        state.orderId = orderId;
        state.isView = false;
        state.isViewItem = false;
    },

    showViewOrder({state}, orderId) {
        state.showEditOrder = true;
        state.orderId = orderId;
        state.isView = true;
        state.isViewItem = true;
    },

    findOrderItemsByOrderId({state, commit}){
        Axios.get(`/sell/orderItem/findOrderItemsByOrderId/${state.orderId}`).then((res) => {
            console.log("查询产品返回：", res);
            commit("Set_Order_Items", res.result);

        })
    },

    removeOrderItems({state}){
        state.orderItems = state.orderItems.filter( it => {
            for(let i = 0; i < state.selectedItems.length; i++){
                if(state.selectedItems[i].id === it.id){
                    return false;
                }
            }
            return true;
        });
        console.log("state.orderItems", state.orderItems);
        state.selectedItems = [];
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
